package com.lzk.pet.service.imp;


import com.lzk.basic.service.impl.BaseServiceImpl;
import com.lzk.pet.domain.PetType;
import com.lzk.pet.mapper.PetTypeMapper;
import com.lzk.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;
    /**
     * 宠物类型无极树
     * @return
     */
    @Override
    public List<PetType> typeTree() {
        //1.查询所有数据，循环遍历放入map
        List<PetType> all = petTypeMapper.loadAll();
        HashMap<Long, PetType> map = new HashMap<>();
        for (PetType petType : all) {
            map.put(petType.getId(), petType);
        }
        //2.创建一个空的数组为宠物子集树,将数据传给前端
        ArrayList<PetType> typeTree = new ArrayList<>();
        for (PetType petType : all) {//宠物类型
            if (petType.getPid() == null) {
                typeTree.add(petType);
            }else {//子集就放入child_department
                Long parent_id = petType.getPid();
                PetType parentPetType = map.get(parent_id);
                parentPetType.getChildren().add(petType);
            }
        }

        return typeTree;
    }
}
